package CACA;

import java.util.ArrayList;

public class ModelTestInfo {

	ArrayList<Integer> prediction = new ArrayList<Integer>();
	int[][] confusionMatrix;
	int correct ;
	int incorrect;
	
	public int getConfusionMatrix(int i,int j) {
		return confusionMatrix[i][j];
	}
	public ModelTestInfo(int k)
	{
		confusionMatrix = new int[k][k];
		for (int i = 0; i < k; i++) {
			for (int j = 0; j < k; j++) {
				confusionMatrix[i][j] = 0;
			}
			
		}
	}
	public void add(ModelTestInfo info)
	{
		correct += info.correct;
		incorrect += info.incorrect;
		int k=confusionMatrix.length;
		for (int i = 0; i < k; i++) {
			for (int j = 0; j < k; j++) {
				confusionMatrix[i][j] += info.confusionMatrix[i][j];
			}
		}
		prediction.addAll(info.prediction);
	}
	public ArrayList<Integer> getPrediction() {
		return prediction;
	}
	public int getCorrect() {
		return correct;
	}
	public int getIncorrect() {
		return incorrect;
	}
	public int getTotal() {
		return correct+incorrect;
	}
	public double getAccuracy() {
		return (double)correct/(correct+incorrect);
	}
	public double[] getPrecision(){
		double[] precision = new double[confusionMatrix.length];
		for(int i = 0 ;i < precision.length;i++)
		{
			int p = 0;
			for(int j = 0 ;j < precision.length;j++)
			{
				p+= confusionMatrix[j][i];
			}
			precision[i] = (double)confusionMatrix[i][i]/p;
		}
		return precision;
	}
	public double getAvgPrecision(){
		int n = 0;
		double sum = 0;

		for(int i = 0 ;i < confusionMatrix.length;i++)
		{		
			int p = 0;
			int w = 0;
			for(int j = 0 ;j < confusionMatrix.length;j++)
			{
				p+= confusionMatrix[j][i];
				w+= confusionMatrix[i][j];
			}
			if(p>0)
			{
				sum += (double)w*(double)confusionMatrix[i][i]/p;
			}
			n += w;
		}
		return sum/n;
	}
	public double[] getRecall(){
		double[] recall = new double[confusionMatrix.length];
		for(int i = 0 ;i < recall.length;i++)
		{
			int p = 0;
			for(int j = 0 ;j < recall.length;j++)
			{
				p+= confusionMatrix[i][j];
			}
			recall[i] = (double)confusionMatrix[i][i]/p;
		}
		return recall;
	}
	public double getAvgRecall(){
		
		
		int n = 0;
		double sum = 0;
		for(int i = 0 ;i < confusionMatrix.length;i++)
		{
			int p = 0;
			for(int j = 0 ;j < confusionMatrix.length;j++)
			{
				p+= confusionMatrix[i][j];
			}
			if(p>0)
			{
				sum += (double)p*(double)confusionMatrix[i][i]/p;
				n += p;
			}
		}
		return sum/n;
	}

}
